package com.leapp.sk.ui.activity.join

import android.content.Intent
import android.os.Bundle
import android.view.View
import cc.vv.lkbasecomponent.http.LKHttp
import cc.vv.lkbasecomponent.http.lib.setting.Settings
import cc.vv.lkbasecomponent.util.LKSPUtil
import cc.vv.lklibrary.http.base.okhttpfactory.plugins.httpplugin.MediaTypeWrap
import com.example.baselibrary.util.LKToastUtil
import com.example.mvp.activity.BaseActivityMVP
import com.example.mvp.binder.BaseDataBinder
import com.leapp.sk.R
import com.leapp.sk.app.JOIN_CLASS
import com.leapp.sk.app.SEARCH_CLASS_LIST
import com.leapp.sk.bean.SearchClassListEntity
import com.leapp.sk.bean.request.join.JoinClassRequestEntity
import com.leapp.sk.bean.request.login.SearchClassListRequestEntity
import com.leapp.sk.bean.response.join.JoinClassResponseEntity
import com.leapp.sk.bean.response.login.SearchClassListResponseEntity
import com.leapp.sk.binder.join.SearchClassBinder
import com.leapp.sk.inter.JoinClassInterface
import com.leapp.sk.inter.SearchInterface
import com.leapp.sk.sever.login.ClassListSever
import com.example.util.finallist.BABY_ID
import com.example.util.finallist.IS_BABY_ARCHIVES
import com.example.util.finallist.KEY_CLASS_ID
import com.example.util.finallist.SKCODE_200
import com.leapp.sk.viewdelegate.join.SearchClassDelegate

/**
 * 搜索班级
 * @author  chaohaipeng  on 2018/7/11.
 */
class SearchClassActivity:BaseActivityMVP<SearchClassDelegate>(), SearchInterface, JoinClassInterface {

    /**
     * 集合数据
     */
  val mList= mutableListOf<SearchClassListEntity>()

    /**
     * 是否是宝贝档案
     */
   var isBaby: Boolean?=false
    /**
     * 宝贝Id
     */
    var childId:String?=""

    override fun initDate() {
        isBaby= intent?.getBooleanExtra(IS_BABY_ARCHIVES,false)
        childId= intent?.getStringExtra(BABY_ID)

//        for (i in 0..3){
//            mList.add(SearchClassListEntity())
//        }
        viewDelegate.initAdapter(mList,isBaby)
       httpRequestList("")
    }
    override fun joinClass(data: SearchClassListEntity?) {
        httpJoinClass(data)

    }

    override fun bindEvenListener() {
        viewDelegate.setSearchInterfaces(this)
        viewDelegate.setJoinClassInterfaces(this)
        viewDelegate.setOnClickListener(View.OnClickListener {
            viewDelegate.clearContent()
        }, R.id.iv_asc_clear)
        viewDelegate.setOnClickListener(View.OnClickListener {
           finish()
        }, R.id.tv_asc_calcel)
        viewDelegate.itemEvent()



    }

    override fun getDelegateClass(): Class<SearchClassDelegate> {
        return SearchClassDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return SearchClassBinder()

    }

    override fun initView(p0: Bundle?) {
        viewDelegate.setClearDatavisible()
        viewDelegate.notifyDataSetChanged()
    }
    override fun searchData(data: String) {
        httpRequestList(data)
    }

    /**
     * 列表数据
     */
    private fun httpRequestList(className: String?) {
        if (className==null){
            return
        }
        val classListRequestEntity = SearchClassListRequestEntity()
        classListRequestEntity.searchValue=className
        LKHttp.post(SEARCH_CLASS_LIST, classListRequestEntity, SearchClassListResponseEntity::class.java, object : BaseCallBack<SearchClassListResponseEntity>(this) {
            override fun onSuccess(url: String?, resultText: String?, obj: SearchClassListResponseEntity?) {
                super.onSuccess(url, resultText, obj)
                if (obj?.code == SKCODE_200) {
                    mList.clear()
                    val listData = ClassListSever.getInstance().getListData(obj)
                    mList.addAll(listData)
                    viewDelegate.notifyDataSetChanged()
                }else{
                    LKToastUtil.showToastShort(obj?.message)
                }
            }
            override fun onFailure(url: String?, isLoading: Boolean, exceptionStr: String?) {
                super.onFailure(url, isLoading, exceptionStr)
                LKToastUtil.showToastShort(resources.getString(R.string.app_net_erro))
            }
        }
                , true, Settings(MediaTypeWrap.MEDIA_TYPE_FORM))

    }

    /**
     * 加入班级
     */
    private fun httpJoinClass(data: SearchClassListEntity?) {
        if (data==null||childId==null){
            return
        }
        val joinClassRequestEntity = JoinClassRequestEntity()
        joinClassRequestEntity.parentId=joinClassRequestEntity.userId
        joinClassRequestEntity.classId=data.id!!
        joinClassRequestEntity.childId=childId!!
        LKHttp.post(JOIN_CLASS, joinClassRequestEntity, JoinClassResponseEntity::class.java, object : BaseCallBack<JoinClassResponseEntity>(this) {
            override fun onSuccess(url: String?, resultText: String?, obj: JoinClassResponseEntity?) {
                super.onSuccess(url, resultText, obj)
                if (obj?.code == SKCODE_200) {
                    LKSPUtil.getInstance().put(KEY_CLASS_ID,data.id!!)
                    val intent: Intent = Intent(this@SearchClassActivity, ToExamineActivity::class.java)
                    intent.putExtra(IS_BABY_ARCHIVES,isBaby)
                    this@SearchClassActivity.startActivity(intent)
                }else{
                    LKToastUtil.showToastShort(obj?.message)
                }
            }
            override fun onFailure(url: String?, isLoading: Boolean, exceptionStr: String?) {
                super.onFailure(url, isLoading, exceptionStr)
                LKToastUtil.showToastShort(resources.getString(R.string.app_net_erro))
            }
        }
                , true, Settings(MediaTypeWrap.MEDIA_TYPE_FORM))

    }
}